<?php

namespace app\common\model;


class MallGoods extends TimeModel
{

    protected $name = "mall_goods";

    protected $deleteTime = "delete_time";


    public function mallCate()
    {
        return $this->belongsTo('\app\common\model\MallCate', 'cate_id', 'id');
    }

    /**
     * @param int $cate_id
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallGoodsByCateId(int $cate_id = 0, string $field = '*'): array
    {
        if ($cate_id == 0) {
            return [];
        }
        return $this->field($field)
            ->where('cate_id', $cate_id)
            ->where('status', 1)
            ->select()
            ->toArray();
    }

    /**
     * 查询首页商品信息
     * @param array $cate_ids
     * @param int $limit
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsIndexList(array $cate_ids = [],int $limit = 10,string $field='*'): array
    {
        return $this->field($field)
            ->where('cate_id',"in",$cate_ids)
            ->order('sort desc,id desc')
            ->limit($limit)
            ->select()
            ->toArray();
    }

}